package Minesweeper;

import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import Minesweeper.game.mineMap.mineTile;

public class game {
	protected static int windowWidth = 500;
	protected static int windowHeight = 500;
	protected static int blockSize = 25;
	JFrame frame;
	mineMap map;
	boolean[][] whereTheMinesAre;
	mineTile[][] mineTileArray;
	public static void main(String[] args) {
		game Game = new game();
	}
	game(){
		frame = new JFrame();
		while (windowWidth%blockSize == 0){
			windowWidth++;
		}
		while (windowHeight%blockSize == 0){
			windowHeight++;
		}
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setBounds(100, 100, windowWidth, windowHeight);
		map = new mineMap();
		frame.add(map.getMap());
		frame.setResizable(false);
	}
	public void mineClicked(){
		
	}
	public void checkIfDone(){
	}
	
	class mineMap{
		private JPanel mapPanel;

		mineMap(){
			mineTileArray = new mineTile[windowWidth/blockSize][windowHeight/blockSize];
			whereTheMinesAre = new boolean[windowWidth/blockSize][windowHeight/blockSize];
			for (int y = 0; y < windowWidth/blockSize; y++){
				for (int x = 0; x < windowHeight/blockSize; x++){
					boolean hasMine = false;
					if(y==1&& x==1){
						//hasMine = true;
					}
					if(Math.random()<.1){
						//hasMine = true;
					}
					whereTheMinesAre[y][x]=hasMine;
				}
			}
			mapPanel = new JPanel();
			mapPanel.setLayout(new GridLayout(windowHeight/blockSize, windowWidth/blockSize));
			mapPanel.setBounds(0, 0, frame.getWidth(), frame.getHeight());
			for (int y = 0; y < windowWidth/blockSize; y++){
				for (int x = 0; x < windowHeight/blockSize; x++){
					mineTileArray[x][y] = new mineTile(whereTheMinesAre[x][y], x, y);
					mapPanel.add(mineTileArray[x][y].getTile());
				}
			}
		}
		JPanel getMap(){
			return mapPanel;
		}
		
		class mineTile{
			private boolean flagDisplayed = false;
			private boolean previouslyClicked = false;
			private boolean hasMine;
			private int xPos;
			private int yPos;
			private ImageLabel tile;
			mineTile(boolean hasMineIn, int xPosIn, int yPosIn){
				
				hasMine = hasMineIn;
				xPos = xPosIn;
				yPos = yPosIn;
				System.out.println("New mineTile created at "+xPos+" "+yPos);
				if(hasMine){
					System.out.println("IT HAS A BOMB");
				}else{
					System.out.println("It does not have a bomb");
				}
				tile = new ImageLabel("images/blank.png");
				tile.addMouseListener(new MouseListener() {
					
					@Override
					public void mouseReleased(MouseEvent arg0) {
						if(SwingUtilities.isRightMouseButton(arg0)){
							rightButtonClicked();
						}else{
							leftButtonClicked();
						}
					}
				
					
					@Override
					public void mouseExited(MouseEvent arg0) {
												
					}
					
					@Override
					public void mouseEntered(MouseEvent arg0) {
						
					}
					
					@Override
					public void mouseClicked(MouseEvent arg0) {
						
						
					}

					@Override
					public void mousePressed(MouseEvent e) {
						
					}
				});
			}
			public ImageLabel getTile(){
				return tile;
			}
			public boolean hasMine(){
				return hasMine;
			}
			void rightButtonClicked(){
				if(!previouslyClicked){
					if(flagDisplayed){
						flagDisplayed = false;
						tile.changeImage("images/blank.png");
					}else{
						flagDisplayed = true;
						tile.changeImage("images/flag.png");
					}
				}
			}
			public int xPos(){
				return xPos;
			}
			public int yPos(){
				return yPos;
			}
			void leftButtonClicked(){
				if(!previouslyClicked){
					previouslyClicked = true;
					if(hasMine){
						tile.changeImage("images/mine.png");
						//game.this.mineClicked();
					}else{
						tile.changeImage("images/"+howManyMinesAround()+".png");
					}
				}
			}
			void checkIfZero(){
				if(!previouslyClicked){
					if(howManyMinesAround() == 0){
						this.previouslyClicked = true;
						tile.changeImage("images/"+howManyMinesAround()+".png");
					}
				}
				
			}
			int howManyMinesAround(){
				System.out.println("xPos: "+xPos+"    yPos: "+yPos);
				int xPanels = game.windowWidth/game.blockSize;
				int yPanels = game.windowHeight/game.blockSize;
				int adjMines = 0;
				int minX = Math.min(xPanels -1, xPos+1);
				int minY = Math.min(yPanels - 1, yPos+1);
				for(int i = Math.max(0,  xPos -1); i <= minX; i++){
					for(int j = Math.max(0, yPos -1); j <= minY; j++){
						if(mineTileArray[i][j].hasMine){
							adjMines++;
						}
					}
				}
				if(adjMines == 0){
					try{
						mineTileArray[xPos++][yPos].checkIfZero();
					}catch(Exception e){}
					try{
						mineTileArray[xPos--][yPos].checkIfZero();
					}catch(Exception e){}
					try{
						mineTileArray[xPos][yPos++].checkIfZero();
					}catch(Exception e){}  
					try{
						mineTileArray[xPos][yPos--].checkIfZero();
					}catch(Exception e){}
//					if(Math.random()>.5){
//						try{
//							mineTileArray[xPos++][yPos].checkIfZero();
//						}catch(Exception e){}
//						try{
//							mineTileArray[xPos--][yPos].checkIfZero();
//						}catch(Exception e){}
//					}else{
//						try{
//							mineTileArray[xPos--][yPos].checkIfZero();
//						}catch(Exception e){}
//						try{
//							mineTileArray[xPos++][yPos].checkIfZero();
//						}catch(Exception e){}
//						
//					}
//					if(Math.random()>.5){
//						try{
//							mineTileArray[xPos][yPos++].checkIfZero();
//						}catch(Exception e){}  
//						try{
//							mineTileArray[xPos][yPos--].checkIfZero();
//						}catch(Exception e){}
//					}else{
//						try{
//							mineTileArray[xPos][yPos--].checkIfZero();
//						}catch(Exception e){}
//						try{
//							mineTileArray[xPos][yPos++].checkIfZero();
//						}catch(Exception e){}  
//						
//					}
					
					
				}
				return adjMines;

			}
		}
	} 
}
